<?php
class mailer
{
  var $headers;
  var $multipart = "";
  var $mime = "";
  var $html = "";
  var $body = "";
  var $parts = array();
  var $use_socket = false;
  var $mime_types;
 
  function mailer($headers = "")
  {
    $this->headers = $headers;
    $this->mime_types = array (
                               'gif'  => 'image/gif',
                               'jpg'  => 'image/jpeg',
                               'jpeg' => 'image/jpeg',
                               'jpe'  => 'image/jpeg',
                               'bmp'  => 'image/bmp',
                               'png'  => 'image/png',
                               'tif'  => 'image/tiff',
                               'tiff' => 'image/tiff',
                               'html' => 'text/html',
                               'htm'  => 'text/html',
                               'swf'  => 'application/x-shockwave-flash',
                               'rar'  => 'application/x-rar-compressed',
                               'zip'  => 'application/zip'
                              );
  }

  function destroy()
  {
    $this->parts = array();
    $this->body = "";
    $this->html = "";
    $this->mulripart = "";
    $this->mime = "";
    $this->headers = "";
  }

  function add_html($html = "")
  {
    $this->html .= $html;
  }

  function build_html($orig_boundary, $kod)
  {
    if(strlen($this->html))
    {
     $this->multipart .= "--$orig_boundary\r\n";
     if($kod=='w' || $kod=='win' || $kod=='windows-1251')
       $kod = 'windows-1251';
     else
       $kod = 'koi8-r';
     $this->multipart .= "Content-Type: text/html; charset=$kod\r\n";
     $this->multipart .= "Content-Transfer-Encoding: Quot-Printed\r\n\r\n";
     $this->multipart .= "$this->html\r\n\r\n";
    }
  }

  function add_body($body = "")
  {
    $this->body .= $body;
  }

  function build_body($orig_boundary, $kod)
  {
    if(strlen($this->body))
    {
      $this->multipart .= "--$orig_boundary\r\n";
      if($kod=='w' || $kod=='win' || $kod=='windows-1251')
        $kod = 'windows-1251';
      else
        $kod = 'koi8-r';
      $this->multipart .= "Content-Type: text/plain; charset=$kod\r\n";
      $this->multipart .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
      $this->multipart .= "$this->body\n\n";
    }
  }

  function add_attachment($path = "", $name = "", $c_type = "")
  {
    if(strlen($name) == 0)
      return;
    $fname = $path . '/'. $name;
    if( !file_exists($fname) )
    {
      print "file $fname dosn't exist";
      return;
    }
    $fl = @fopen($fname, "rb");
    if($fl)
    {
      $file = fread($fl, filesize($fname));
      fclose($fl);
      if( !strlen($c_type) )
      {
        $pos = strrpos($name, '.');
        if($pos !== false)
        {
          $ext = strtolower( substr($name, $pos+1) );
          if( isset($this->mime_types[ $ext ]) )
            $c_type = $this->mime_types[ $ext ];
        }
        if(!strlen($c_type))
          $c_type = 'application/octet-stream';
      }
      $this->parts[] = array("body"=>$file, "name"=>$name,"c_type"=>$c_type);
    }
    else
      print "Error read from file $fname";
  }
 
  function remove_attachments()
  {
    $this->parts = array();
  }

  function build_part($i)
  {
    $message_part = "";
    $message_part .= "Content-Type: ".$this->parts[$i]["c_type"];
    if( $this->parts[$i]["name"] != "")
      $message_part .= "; name=\"" . $this->parts[$i]["name"] . "\"\r\n";
    else
      $message_part .= "\r\n";
    $message_part .= "Content-Transfer-Encoding: base64\r\n";
    $message_part .= "Content-Disposition: attachment; filename=\"". $this->parts[$i]["name"]."\"\r\n\r\n";
    $message_part .= chunk_split(base64_encode($this->parts[$i]["body"])) . "\r\n";
    return $message_part;
  }

  function build_message($kod)
  {
    $boundary = "----" . md5(uniqid(time()));
    $this->headers = "MIME-Version: 1.0\r\n";
    $this->headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
    $this->multipart = "";
    $this->multipart .= "This is a MIME encoded message.\r\n\r\n";
    $this->build_body($boundary, $kod);
    $this->build_html($boundary, $kod);

    for($i = (count($this->parts) - 1); $i >= 0; $i--)
      $this->multipart .= "--$boundary\r\n" . $this->build_part($i);
    $this->mime = "$this->multipart--$boundary--\r\n";
  }

  function usesocket($use = true)
  {
    $this->use_socket = $use;
  }

  function send($server, $from, $to, $subject="", $headers="")
  {
    if( strlen($to) == 0 )
      return false;
	$headers = "From: $from\r\nX-Mailer: ArkaSender v.1.0\r\n$headers";
    $headers .= $this->headers;
    if( !$this->use_socket )
    {
      return @mail($to, $subject, $this->mime, $headers);
    }
    else
    {
      $headers .= "Subject: $subject\r\n";
      $fp = @fsockopen($server, 25, &$errno, &$errstr, 30);
      if(!$fp)
      {
        print "Server $server. Connection failed: $errno, $errstr";
        return false;
      }
      fputs($fp, "HELO $server\r\n");
      $resp = fgets($fp, 1024);
      fputs($fp, "MAIL FROM: $from\r\n");
      $resp = fgets($fp, 1024);
      fputs($fp, "RCPT TO: $to\r\n");
      $resp = fgets($fp, 1024);
      fputs($fp, "DATA\r\n");
      $resp = fgets($fp, 1024);
      fputs($fp, "$headers\r\n");
      fputs($fp,$this->mime);
      fputs($fp, "\r\n.\r\nQUIT\r\n");
      while( !feof($fp) )
        $resp .= fgets($fp, 1024);
      fclose($fp);

      return true;
    } // use_socket
  }
} // class mailer
?>

